//
//  ZLQWroteTravelNotesVC.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/4.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

//typealias Block = (string:String) -> Void

class ZLQWroteTravelNotesVC: UIViewController,UITextViewDelegate,UMSocialUIDelegate {

//    var passBlock:Block?
    
    var tableview = UITableView() //tableview
    
    var  headerview:UIView?//header视图
    
    var footerview:UIView?//footer视图
    
    var contentV:UITextView?//textview视图
    
    var toolbar:UIToolbar?//键盘上的自定义按钮
    
    var lab = UILabel()//textview上的label
    
    var umengBtn:UIButton?//分享button
    
    var hintL:UILabel? //提示信息
    
    var datePicker:UIDatePicker?//时间选择器
    
    var dateTF:UITextField? //时间TF
    
    var number = Int()
    
    var zlqpict:ZLQPictureCollecitionViewController!
    
    var string:String = "时间"
    var string1:String = "地点"
    
    var deleteBut = UIButton() //删除datePicker
    
    var number1:Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.groupTableViewBackgroundColor()
        self.navigationItem.title = "写游记"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "HTHome_Menu"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showSideView"))
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("saveAction"))
        
        
        
        addViews()
        addGestureRecognizer()
        
        
//        datePicker创建
        self.datePicker = UIDatePicker(frame: CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,0))
        self.datePicker?.backgroundColor = UIColor.whiteColor()
                    datePicker?.datePickerMode = .DateAndTime
        self.datePicker?.sizeToFit()
        self.datePicker?.locale = NSLocale.systemLocale()
        self.datePicker?.locale = NSLocale.init(localeIdentifier: "ZH_CH")
        self.datePicker?.addTarget(self, action: Selector("datechage:"), forControlEvents: UIControlEvents.ValueChanged)
        self.number = Int(self.view.frame.size.height*0.15)
        let formatter:NSDateFormatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
        self.string = formatter.stringFromDate(self.datePicker!.date)

        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.tableview.reloadData()
        }
        
        
    }
  
    func showSideView()->Void {
        
        self.sideMenuViewController.presentLeftMenuViewController()
    }
//    添加手势
    func addGestureRecognizer() -> Void{
    
        let swipe:UISwipeGestureRecognizer = UISwipeGestureRecognizer()
        swipe.addTarget(self, action: Selector("addAction"))
        swipe.direction = .Up
        self.footerview!.addGestureRecognizer(swipe)

    }
//    手势响应事件
    func addAction() ->Void{
        
        UMSocialSnsService.presentSnsIconSheetView(self, appKey: nil, shareText: contentV?.text, shareImage: nil, shareToSnsNames: nil, delegate: self)
        
    }
    
//    保存事件
    func saveAction() ->Void{
    
    
    }
    
  
//添加控件
    func addViews() {
        tableview.frame = self.view.bounds
        tableview.bounces = false
        self.view.addSubview(tableview)
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.registerClass(ZLQWroteTravelNotesCell.classForCoder(), forCellReuseIdentifier: "cell")
    
       
//        header 视图的添加
        headerview = UIView()
        headerview!.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.4)
        headerview!.backgroundColor = UIColor.groupTableViewBackgroundColor()
        self.view.addSubview(headerview!)
        
        
      let  zlqpict = ZLQPictureCollecitionViewController()
        self.addChildViewController(zlqpict)
           headerview?.addSubview(zlqpict.view)
//        let a:CGSize = (zlqpict.collectionview?.sizeThatFits(CGSizeMake((headerview?.frame.size.width)!*0.9,CGFloat(MAXFLOAT))))!
//        zlqpict.view.frame = CGRectMake(headerview!.frame.size.width*0.04, headerview!.frame.size.height*0.4, headerview!.frame.size.width*0.9, a.height)
//         headerview?.addSubview(zlqpict.view)
         number1 = number
      
        zlqpict.collectionview!.frame = CGRectMake(headerview!.frame.size.width*0.04, headerview!.frame.size.height*0.55, headerview!.frame.size.width*0.9, self.view.frame.size.height*0.16)
        
//        zlqpict.view.backgroundColor = UIColor.redColor()
     

        
        contentV = UITextView()
        contentV?.frame = CGRectMake(headerview!.frame.size.width*0.04,headerview!.frame.size.height*0.12, headerview!.frame.size.width*0.9, self.headerview!.frame.size.height*0.4)
        contentV?.delegate = self
        contentV?.backgroundColor = UIColor.whiteColor()
         contentV?.keyboardType = UIKeyboardType.Default
        contentV?.returnKeyType = .Default
        headerview?.addSubview(contentV!)
        tableview.tableHeaderView = headerview
     
        
        lab.frame = CGRectMake(headerview!.frame.size.width*0.04,headerview!.frame.size.height*0.03, headerview!.frame.size.width*0.9, self.headerview!.frame.size.height*0.06)
        lab.text = "分享新鲜事:"
//        lab.backgroundColor = UIColor.greenColor()
        headerview?.addSubview(lab)
  

        
//        键盘上的回收按钮
       toolbar = UIToolbar(frame: CGRectMake(0,0,350,40))
        toolbar?.backgroundColor = UIColor.whiteColor()
        toolbar?.barStyle = .Black

        let doneButton:UIBarButtonItem = UIBarButtonItem(title: "隐藏键盘", style: UIBarButtonItemStyle.Done, target: self, action: Selector("dismissKeyBoard"))
        let array = [doneButton]
        toolbar?.setItems(array, animated: true)
       contentV?.inputAccessoryView = toolbar
        
        
//        footer 视图的添加
        footerview = UIView()
        footerview?.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.34)
//        footerview?.backgroundColor = UIColor.groupTableViewBackgroundColor()
        tableview.tableFooterView = footerview
        
        hintL = UILabel(frame: CGRectMake(0,self.footerview!.frame.size.height*0.07,(self.footerview?.frame.size.width)!,self.footerview!.frame.size.height*0.1))
        hintL?.text = "向上滑动分享更多!"
        hintL?.textAlignment = .Center
        hintL?.alpha = 0.5
        footerview?.addSubview(hintL!)
        
    
    }
    
    func dismissKeyBoard() {
    
        self.contentV?.resignFirstResponder()
    }
    

    

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
    
extension ZLQWroteTravelNotesVC:UITableViewDataSource,UITableViewDelegate {
   
//    headerView
//    func tableView(tableView: UITableView,
//        viewForHeaderInSection section: Int) -> UIView? {
//            
//            let headerview = UIView()
//            if section == 0 {
//            
//                headerview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.4)
//                headerview.backgroundColor = UIColor.redColor()
//            }
//
//            
//      return headerview
//    }
//   header高度
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
//        if section == 0{
//            return 350
//        } else if section == 1 {
//            return 0
//        }else{
//        return 1
//        }
        return 2
    }
    
//    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//        
//        if section == 1 {
//            
////            if datePicker != nil {
////                return datePicker
////            } else {
////                return nil
////            }
//        }
//        else {
//        return nil
//        }
//    
//    }
//
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
//        if section == 1 {
//        return CGFloat(number)
//        } else{
        return 5
//        }
        
      
    }
    
//    分区数
     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2;
    }
    

//    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return 100
//    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return self.view.frame.size.height*0.05;
    }
    
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
//       self.tableview.reloadData()
        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? ZLQWroteTravelNotesCell
        if cell == nil {
        cell = ZLQWroteTravelNotesCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        }
        
        cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            
            if indexPath.section == 0{
                
                cell?.titleL?.text = self.string1
                

            } else if indexPath.section == 1 {
                
                cell?.titleL?.text = self.string

            }

 
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if indexPath.section == 1{
        deleteBut = UIButton(type: UIButtonType.Custom)
        deleteBut.frame = CGRectMake(self.view.frame.size.width, self.view.frame.size.height*0.62, self.view.frame.size.width*0.3, self.view.frame.size.height*0.1)
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.3)
        UIView.setAnimationDelegate(self)
        datePicker?.frame = CGRectMake(0, self.view.frame.size.height*0.7, self.view.frame.size.width, self.view.frame.size.height*0.3)
        self.view.addSubview(datePicker!)
        deleteBut.frame = CGRectMake(self.view.frame.size.width*0.7, self.view.frame.size.height*0.62, self.view.frame.size.width*0.3, self.view.frame.size.height*0.1)
        deleteBut.setTitle("Done", forState: UIControlState.Normal)
        deleteBut.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        self.view.addSubview(deleteBut)
        UIView.commitAnimations()
        deleteBut.addTarget(self, action: Selector("deleteAction"), forControlEvents: UIControlEvents.TouchUpInside)
        } else {
            let a:ZLQSearchController = ZLQSearchController()
            
//            let zlqsearch:ZLQSearchController = ZLQSearchController()
//         let cell = tableView.cellForRowAtIndexPath(indexPath) as! ZLQWroteTravelNotesCell
            
            a.passBlock = {
                (str) -> Void in
                print(str)
                self.string1 = str
                
                tableView.reloadData()
            }

            
            self.navigationController?.pushViewController(a, animated: true)
//            self.presentViewController(a, animated: true, completion: nil)
        
        }
    
        
        
    }
    func deleteAction() {
    
        deleteBut.removeFromSuperview()
        datePicker?.removeFromSuperview()
        
    }
    
    func datechage(pick:UIDatePicker){
    
        let formatter:NSDateFormatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
        self.string = formatter.stringFromDate(pick.date)
        self.tableview.reloadData()
        
    }
    
    
}

